#include<iostream>
#include<string.h>
#define MAX 1000
using namespace std;

void ShowMenu()//显示目录
{
	cout << "**********************" << endl;
	cout << "**** 1.添加联系人 ****" << endl;
	cout << "**** 2.显示联系人 ****" << endl;
	cout << "**** 3.删除联系人 ****" << endl;
	cout << "**** 4.查找联系人 ****" << endl;
	cout << "**** 5.修改联系人 ****" << endl;
	cout << "**** 6.清空联系人 ****" << endl;
	cout << "**** 0.退出通讯录 ****" << endl;
	cout << "**********************" << endl;
}
struct Person	//联系人结构体
{
	string Name;//姓名
	int Sex=0;//性别 1 男 2 女
	int Age=0;//年龄
	string Phone;//电话
	string Addr;//住址
};
struct Addressbooks//通讯录结构体
{
	//通讯录中保存的联系人数组
	struct Person PersonArray[MAX];
	//通讯录中当前的联系人个数
	int Size=0;
};
void AddPerson(Addressbooks* abs)//1.添加联系人
{
	if (abs->Size == MAX)
	{
		cout << "通讯录已满，无法添加！" << endl;
	}
	else {
		cout << "请输入姓名：" << endl;
		string name;
		cin >> name;
		abs->PersonArray[abs->Size].Name = name;
		
		cout << "请输入性别：" << endl;
		cout << "1  ---  男" << endl;
		cout << "2  ---  女" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->PersonArray[abs->Size].Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入（1 或 2）" << endl;
		}

		cout << "请输入年龄：" << endl;
		int age = 0;
		cin >> age;
		abs->PersonArray[abs->Size].Age = age;

		cout << "请输入手机号：" << endl;
		string phone;
		cin >> phone;
		abs->PersonArray[abs->Size].Phone = phone;

		cout << "请输入住址：" << endl;
		string addr;
		cin >> addr;
		abs->PersonArray[abs->Size].Addr = addr;

		//更新通讯录
		abs->Size++;
		
		cout << "添加成功!" << endl;
		system("pause");//请按任意键继续
		system("CLS");//清屏
	}
}
void ShowPersom(Addressbooks* abs)//2.显示所有联系人
{
	//判断通讯录中是否为空
	if (abs->Size == 0)
	{
		cout << "当前记录为空" << endl;
	}
	else {
		for (int i = 0; i < abs->Size; i++)
		{
			cout << "姓名：" << abs->PersonArray[i].Name<< "\t";
			cout << "性别：" << ( abs->PersonArray[i].Sex == 1 ? "男" : "女" ) << "\t";
			cout << "年龄：" << abs->PersonArray[i].Age<< "\t";
			cout << "电话：" << abs->PersonArray[i].Phone<< "\t";
			cout << "地址：" << abs->PersonArray[i].Addr<< endl;
		}
	}
	system("pause");
	system("CLS");
}
int isExist(Addressbooks* abs,string name)//检测联系人是否存在
{
	for (int i = 0; i < abs->Size; i++)
	{
		//找到联系人，并返回
		if (abs->PersonArray[i].Name == name)
		{
			return i;
		}
	}
	return -1;
}
void DeletePerson(Addressbooks* abs)//删除联系人
{
	cout << "请输入您要删除的联系人:" << endl;
	string name;
	cin >> name;

	//ret == -1 未查到
	//ret != -1 已查到
	int ret = isExist(abs, name);
	if (ret == -1)
	{
		//查找到人,进行删除操作
		for (int i = ret; i < abs->Size;i++)
		{
			//数据前移
			abs->PersonArray[i] = abs->PersonArray[i + 1];
		}
		//数据更新
		abs->Size--;
		cout << "删除成功" << endl;
	}
	else {
		cout << "查无此人" << endl;
	}
}
void FindPerson(Addressbooks* abs)//查找联系人
{
	cout << "请输入您所查找的联系人" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1)
	{
		cout << "姓名：" << abs->PersonArray[ret].Name << "\t";
		cout << "性别：" << abs->PersonArray[ret].Sex << "\t";
		cout << "年龄：" << abs->PersonArray[ret].Age << "\t";
		cout << "电话：" << abs->PersonArray[ret].Phone << "\t";
		cout << "地址：" << abs->PersonArray[ret].Addr << endl;
	}
	else{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}
void MoidfyPerson(Addressbooks* abs)//修改联系人
{
	cout << "请输入要修改的联系人" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);
	if (ret != -1)
	{
		cout << "请输入姓名：" << endl;
		string name;
		cin >> name;
		abs->PersonArray[abs->Size].Name = name;

		cout << "请输入性别：" << endl;
		cout << "1  ---  男" << endl;
		cout << "2  ---  女" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->PersonArray[abs->Size].Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入（1 或 2）" << endl;
		}

		cout << "请输入年龄：" << endl;
		int age = 0;
		cin >> age;
		abs->PersonArray[abs->Size].Age = age;

		cout << "请输入手机号：" << endl;
		string phone;
		cin >> phone;
		abs->PersonArray[abs->Size].Phone = phone;

		cout << "请输入住址：" << endl;
		string addr;
		cin >> addr;
		abs->PersonArray[abs->Size].Addr = addr;

		cout << "修改成功" << endl;
	}
	else {
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}
void CleanPerson(Addressbooks* abs)
{
	abs->Size = 0;
	cout << "联系人已清空" << endl;
	system("pause");
	system("cls");
}
int main()
{
	//创建通讯录结构体变量
	Addressbooks abs;
	//初始化通讯录中当前人员个数
	abs.Size; 
	int Judge;//创建用户选择的变量
	while (true) //while循环保证返回通讯录菜单
	{
		//目录调用
		ShowMenu();
		cout << "请输入你的选择：" << endl;
		cin >> Judge;
		switch (Judge)
		{
		case 1://添加联系人
			AddPerson(&abs);//利用地址传递,可以修饰实参
			break;
		case 2://显示联系人
			ShowPersom(&abs);
			break;
		case 3://删除联系人
			/*
		{
			cout << "请输入联系人姓名:" << endl;
			string name;
			cin >> name;
			if (isExist(&abs, name) == -1)
			{
				cout << "查无此人" << endl;
			}
			else {
				cout << "找到此人" << endl;
			}
		}
			*/
			DeletePerson(&abs);
			break;
		case 4://查找联系人
			FindPerson(&abs);
			break;
		case 5://修改联系人
			MoidfyPerson(&abs);
			break;
		case 6://清空联系人
			CleanPerson(&abs);
			break;
		case 0://退出通讯录
			cout << "欢迎下次使用!" << endl;
			system("pause");
			return 0;
			break;
		}
	}
	system("pause");//请按任意键继续
	return 0;
}